07 / 10

How do you view container logs in Docker?

In Docker, container logs are viewed using the docker logs command. It streams or displays the stdout and stderr output of a container. Docker provides rich options to filter logs by time, follow live output, show timestamps, and limit the number of lines — making it the primary tool for debugging and monitoring containerized applications.

Key Concepts Before Viewing Logs
  1. 1

    Docker captures everything written to stdout and stderr as container logs

  2. 2

    Logs from files inside the container are NOT captured by docker logs

  3. 3

    The default logging driver is json-file — stores logs as JSON on the host

  4. 4

    docker logs works for both running and stopped containers

  5. 5

    Logs persist until the container is removed or logs are rotated

  6. 6

    Container name or container ID can be used interchangeably

Basic Log Commands
Follow Live Logs (Real-Time Streaming)
Show Last N Lines (Tail)
Show Timestamps with Logs
Filter Logs by Time
Search and Filter Logs with grep
Redirect Logs to a File
Docker Compose Logs
Logging Drivers — Beyond json-file
Log Rotation — Preventing Disk Full Issues
Find Log File Location on Host
Common Flags Summary
  1. 1

    docker logs <container> — show all logs

  2. 2

    docker logs -f <container> — follow/stream live logs

  3. 3

    docker logs -t <container> — show logs with timestamps

  4. 4

    docker logs --tail N <container> — show last N lines only

  5. 5

    docker logs --since Xm <container> — logs from last X minutes/hours

  6. 6

    docker logs --until <time> — logs before a specific time

  7. 7

    docker logs -tf --tail 100 — most useful debug combo

Key Takeaways
  1. 1

    docker logs only captures stdout and stderr — app must log to these streams

  2. 2

    docker logs -f is the go-to command for real-time debugging of live containers

  3. 3

    Pipe docker logs to grep for powerful filtering — docker has no built-in search

  4. 4

    Use --since and --until to narrow logs to a specific incident time window

  5. 5

    Configure log rotation with max-size and max-file to prevent disk exhaustion

  6. 6

    docker logs does NOT work with non json-file drivers like fluentd or awslogs

  7. 7

    In production, ship logs to a centralized system — ELK Stack, CloudWatch, Datadog, Loki